﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MachineStateOnline
{
    public static  class DBHelper
    {
        public static StreamWriter sw = null;
        public static SqlConnection SqlK1Obj = null;
        public static SqlConnection SqlK2Obj = null;
        public static bool K1ConnFailureTag = false;
        public static bool K2ConnFailureTag = false;

        public static DataTable? K1ExecSQLReader(string sql)
        {
            // TODO

            int t1 = 0;
            //如果连接出现异常重连3次。
            while (t1 < 3)
            {
                //创建SqlDataAdapter对象并执行sql语句
                try
                {
                    DataTable dt = new DataTable();
                    SqlCommand command = new SqlCommand(sql,SqlK1Obj);
                    SqlDataReader reader = command.ExecuteReader();
                    dt.Load(reader);
                    reader.Close();
                    return dt;
                }
                catch (Exception ex)
                {
                    sw.WriteLine(DateTime.Now.ToString("f") + "------:K1服务器执行SQL异常 ; 详细: " + ex.Message);
                    sw.Flush();
                    OpenDBK1();
                    Thread.Sleep(200);
                }
                t1++;
            }
            return null;
        }

        public static void K1ExeNotRead(string sql)
        {
            // TODO

            int t1 = 0;
            //如果连接出现异常重连3次。
            while (t1 < 3)
            {
                //创建SqlDataAdapter对象并执行sql语句
                try
                {
                    DataTable dt = new DataTable();
                    SqlCommand command = new SqlCommand(sql, SqlK1Obj);
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    sw.WriteLine(DateTime.Now.ToString("f") + "------:K1服务器执行SQL异常 ; 详细: " + ex.Message);
                    sw.Flush();
                    OpenDBK1();
                    Thread.Sleep(200);
                }
                t1++;
            }
        }


        public static DataTable? K2ExecSQLReader(string sql)
        {
            int t1 = 0;
            //如果连接出现异常重连3次。
            while (t1 < 3)
            {
                //创建SqlDataAdapter对象并执行sql语句
                try
                {
                    DataTable dt = new DataTable();
                    SqlCommand command = new SqlCommand(sql, SqlK2Obj);
                    SqlDataReader reader = command.ExecuteReader();
                    dt.Load(reader);
                    reader.Close();
                    return dt;
                }
                catch (Exception ex)
                {
                    sw.WriteLine(DateTime.Now.ToString("f") + "------:K2服务器执行SQL异常 ; 详细: " + ex.Message);
                    sw.Flush();
                    OpenDBK2();
                    //ex.Message
                    Thread.Sleep(200);
                }
                t1++;
            }
            return null;
        }
        public static void OpenDBK1()
        {
            //https://blog.csdn.net/HerryDong/article/details/106195206
            //var myKeyValue = Configuration["MyKey"];
            //var title = Configuration["Position:Title"];
            //var name = Configuration["Position:Name"];
            //var defaultLogLevel = Configuration["Logging:LogLevel:Default"];

            var k1str = ConfigManager.GetSection("K1DBConnStr");

            //连接数据库
            if (k1str == null)
                k1str = @"Data Source=DESKTOP-GSQONVR;Initial Catalog=SYC_NSJ1;uid=sa; pwd=sa123";
            try
            {
                if (SqlK1Obj == null||SqlK1Obj.State != ConnectionState.Open)
                {
                    SqlK1Obj = new SqlConnection(k1str);
                    //打开数据库连接
                    SqlK1Obj.Open();
                    K1ConnFailureTag = false;
                }
                return;
            }
            catch (Exception ex)
            {
                sw.WriteLine(DateTime.Now.ToString("f") + "------: 一科服务器连接失败 ; 详情:" + ex.Message) ;
                sw.Flush();
                K1ConnFailureTag = true;
            }

            int t1 = 0;
            while (K1ConnFailureTag)
            {
                try
                {
                    SqlK1Obj.Open();
                    K1ConnFailureTag = false;
                    break;
                }
                catch(Exception ex)
                {
                    sw.WriteLine(DateTime.Now.ToString("f") + "------: 一科服务器连接失败 ; 详情:" + ex.Message);
                    sw.Flush();
                    K1ConnFailureTag = true;
                }

                t1++;

                if (t1 >= 2)
                    break;
                Thread.Sleep(400);
            }

        }

        public static void OpenDBK2()
        {
            var k2str = ConfigManager.GetSection("K2DBConnStr");
            if (k2str == null)
                k2str = @"Data Source=DESKTOP-MA90C9S;Initial Catalog=SYC_NSJ2;uid=sa; pwd=sa123";
            try
            {
                if (SqlK2Obj == null||SqlK2Obj.State != ConnectionState.Open)
                {
                    SqlK2Obj = new SqlConnection(k2str);
                    //打开数据库连接
                    SqlK2Obj.Open();
                    K2ConnFailureTag = false;
                }
                
                return;
            }
            catch (Exception ex)
            {
                sw.WriteLine(DateTime.Now.ToString("f") + "------: 二科服务器连接失败 ; 详情:" + ex.Message) ;
                sw.Flush();
                K2ConnFailureTag = true;
            }


            int t1 = 0;
            while (K2ConnFailureTag)
            {
                try
                {
                    SqlK2Obj.Open();
                    K2ConnFailureTag = false;
                    break;
                }
                catch (Exception ex)
                {
                    sw.WriteLine(DateTime.Now.ToString("f") + "------: 二科服务器连接失败 ; 详情:" + ex.Message);
                    sw.Flush();
                    K2ConnFailureTag = true;
                }

                t1++;

                if (t1 >= 2)
                    break;
                Thread.Sleep(400);
            }

        }


    }
}
